package com.kye.express.business.jd.after_sale;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.business.jd.after_sale.fragment.JDAfterSaleApplyFragment;
import com.kye.express.business.jd.after_sale.fragment.JDAfterSaleReturnModeFragment;
import com.kye.express.business.jd.order_detail.model.JDOrderDetailModel;
import com.kye.express.databinding.ActivityJdafterSaleMainBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.model.bean.jd.order_details.JDGoodsAfterSaleTypeBean;
import com.kye.model.bean.user.User;

public class JDAfterSaleMainActivity extends BaseToolbarActivity implements JDAfterSaleMainConstract.JDAfterSaleMainView {

    private final static String TAG_ORDER_DETAIL = "TAG_ORDER_DETAIL";
    private JDOrderDetailModel orderDetailModel;
    private ActivityJdafterSaleMainBinding binding;

    public static Intent newIntent(Context context, JDOrderDetailModel orderDetailModel) {
        Intent intent = new Intent(context, JDAfterSaleMainActivity.class);
        intent.putExtra(TAG_ORDER_DETAIL, orderDetailModel);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_jdafter_sale_main);
        if (savedInstanceState == null) {
            orderDetailModel = getIntent().getParcelableExtra(TAG_ORDER_DETAIL);
        } else {
            orderDetailModel = savedInstanceState.getParcelable(TAG_ORDER_DETAIL);
        }
        initToolbar();
        initViews();
        initEvents();
        initData();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(TAG_ORDER_DETAIL, orderDetailModel);
    }

    private void initToolbar() {
        ToolbarManager.setToolbar(this, binding.toolbar);
    }

    private void initViews() {

    }

    private void initEvents() {

    }

    private void initData() {
        addFragmentStep1ForApply();
    }

    @Override
    public void onUser(User user) {

    }

    public void addFragmentStep1ForApply() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        JDAfterSaleApplyFragment jdAfterSaleApplyFragment = JDAfterSaleApplyFragment.newInstance(orderDetailModel);
        ft.add(R.id.container, jdAfterSaleApplyFragment);
        ft.addToBackStack(null);
        ft.commit();
        getSupportActionBar().setTitle("申请售后服务");
    }

    /**
     * 跳转到提交界面
     * @param orderDetailModel
     * @param selectedAfterSaleType
     * @param descriptions
     */
    public void toSubmitPage(JDOrderDetailModel orderDetailModel, JDGoodsAfterSaleTypeBean selectedAfterSaleType, String descriptions) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        JDAfterSaleReturnModeFragment jdAfterSaleReturnModeFragment = JDAfterSaleReturnModeFragment.newInstance(orderDetailModel, selectedAfterSaleType, descriptions);
        ft.replace(R.id.container, jdAfterSaleReturnModeFragment);
        ft.addToBackStack(null);
        ft.commit();
        getSupportActionBar().setTitle("商品回退方式");
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStackImmediate();
            getSupportActionBar().setTitle("申请售后服务");
        } else {
            finish();
        }
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoaded() {

    }
}
